package com.vehicle.manage.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.vehicle.manage.domain.VehicleFence;
import com.vehicle.manage.mapper.VehicleFenceMapper;
import com.vehicle.manage.service.IVehicleFenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 围栏管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-29
 */
@Service
public class VehicleFenceServiceImpl implements IVehicleFenceService
{
    @Autowired
    private VehicleFenceMapper vehicleFenceMapper;

    /**
     * 查询围栏管理
     * 
     * @param id 围栏管理主键
     * @return 围栏管理
     */
    @Override
    public VehicleFence selectVehicleFenceById(String id)
    {
        return vehicleFenceMapper.selectVehicleFenceById(id);
    }

    /**
     * 查询围栏管理列表
     * 
     * @param vehicleFence 围栏管理
     * @return 围栏管理
     */
    @Override
    public List<VehicleFence> selectVehicleFenceList(VehicleFence vehicleFence)
    {
        return vehicleFenceMapper.selectVehicleFenceList(vehicleFence);
    }

    /**
     * 新增围栏管理
     * 
     * @param vehicleFence 围栏管理
     * @return 结果
     */
    @Override
    public int insertVehicleFence(VehicleFence vehicleFence)
    {
        vehicleFence.setCreateTime(DateUtils.getNowDate());
        vehicleFence.setId(IdUtils.fastSimpleUUID());
        return vehicleFenceMapper.insertVehicleFence(vehicleFence);
    }

    /**
     * 修改围栏管理
     * 
     * @param vehicleFence 围栏管理
     * @return 结果
     */
    @Override
    public int updateVehicleFence(VehicleFence vehicleFence)
    {
        vehicleFence.setUpdateTime(DateUtils.getNowDate());
        return vehicleFenceMapper.updateVehicleFence(vehicleFence);
    }

    /**
     * 批量删除围栏管理
     * 
     * @param ids 需要删除的围栏管理主键
     * @return 结果
     */
    @Override
    public int deleteVehicleFenceByIds(String[] ids)
    {
        return vehicleFenceMapper.deleteVehicleFenceByIds(ids);
    }

    /**
     * 删除围栏管理信息
     * 
     * @param id 围栏管理主键
     * @return 结果
     */
    @Override
    public int deleteVehicleFenceById(String id)
    {
        return vehicleFenceMapper.deleteVehicleFenceById(id);
    }

    @Override
    public int saveFenceData(VehicleFence vehicleFence) {
        return vehicleFenceMapper.saveFenceData(vehicleFence);
    }

//    @Override
//    public int findAll() {
//        vehicleFenceMapper.findAll();
//        for (Fence fence : fences) {
//            if (isInPolygon(lng, lat, fence.getPoints())) {
//                // 触发报警（可在此处调用发送短信/邮件等的代码）
//            }
//        }
//    }

    private boolean isInPolygon(Double lng, Double lat, List<Double[]> points) {
        int count = 0;
        for (int i = 0, j = points.size() - 1; i < points.size(); i++) {
            Double[] pi = points.get(i);
            Double[] pj = points.get(j);
            if ((pi[1] < lat && pj[1] >= lat) || (pj[1] < lat && pi[1] >= lat)) {
                double x = pi[0] + (lat - pi[1]) / (pj[1] - pi[1]) * (pj[0] - pi[0]);
                if (x < lng) {
                    count++;
                }
            }
            j = i;
        }
        return count % 2 == 1;
    }
}
